/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.diego.games.tetris.visual;

import br.diego.games.tetris.TetrisGameCanvas;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.netbeans.microedition.lcdui.SplashScreen;


/**
 * @author diego
 */
public class MainMIDlet extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private TetrisGameCanvas gameCanvas;
    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private SplashScreen splashScreen;
    private List list;
    private Command itemPlay;
    private Command itemInstructions;
    private Command itemQuit;
    private Command exitCommand;
    private Command itemCommand;
    private Command exitCommand1;
    private Command okCommand;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The MainMIDlet constructor.
     */
    public MainMIDlet() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getList());//GEN-LINE:|3-startMIDlet|1|3-postAction
    // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
    // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction

        // write pre-action user code here
        if (displayable == list) {//GEN-BEGIN:|7-commandAction|1|51-preAction
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|1|51-preAction
                // write pre-action user code here
                listAction();//GEN-LINE:|7-commandAction|2|51-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|19-preAction
        } else if (displayable == splashScreen) {
            if (command == SplashScreen.DISMISS_COMMAND) {//GEN-END:|7-commandAction|3|19-preAction
                // write pre-action user code here
//GEN-LINE:|7-commandAction|4|19-postAction
            // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
        }//GEN-END:|7-commandAction|5|7-postCommandAction
    // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|6|
    //</editor-fold>//GEN-END:|7-commandAction|6|




    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: splashScreen ">//GEN-BEGIN:|17-getter|0|17-preInit
    /**
     * Returns an initiliazed instance of splashScreen component.
     * @return the initialized component instance
     */
    public SplashScreen getSplashScreen() {
        if (splashScreen == null) {//GEN-END:|17-getter|0|17-preInit
            // write pre-init user code here
            splashScreen = new SplashScreen(getDisplay());//GEN-BEGIN:|17-getter|1|17-postInit
            splashScreen.setTitle("Tetris");
            splashScreen.setCommandListener(this);
            splashScreen.setFullScreenMode(true);
            splashScreen.setText("Desenvolvidor por Diego F Caetano \n");
            splashScreen.setTimeout(2000);//GEN-END:|17-getter|1|17-postInit
        // write post-init user code here
        }//GEN-BEGIN:|17-getter|2|
        return splashScreen;
    }
    //</editor-fold>//GEN-END:|17-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemPlay ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of itemPlay component.
     * @return the initialized component instance
     */
    public Command getItemPlay() {
        if (itemPlay == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            itemPlay = new Command("1", Command.ITEM, 0);//GEN-LINE:|29-getter|1|29-postInit
        // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return itemPlay;
    }
    //</editor-fold>//GEN-END:|29-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemInstructions ">//GEN-BEGIN:|31-getter|0|31-preInit
    /**
     * Returns an initiliazed instance of itemInstructions component.
     * @return the initialized component instance
     */
    public Command getItemInstructions() {
        if (itemInstructions == null) {//GEN-END:|31-getter|0|31-preInit
            // write pre-init user code here
            itemInstructions = new Command("2", Command.ITEM, 0);//GEN-LINE:|31-getter|1|31-postInit
        // write post-init user code here
        }//GEN-BEGIN:|31-getter|2|
        return itemInstructions;
    }
    //</editor-fold>//GEN-END:|31-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemQuit ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initiliazed instance of itemQuit component.
     * @return the initialized component instance
     */
    public Command getItemQuit() {
        if (itemQuit == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            itemQuit = new Command("3", Command.ITEM, 0);//GEN-LINE:|33-getter|1|33-postInit
        // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return itemQuit;
    }
    //</editor-fold>//GEN-END:|33-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">//GEN-BEGIN:|40-getter|0|40-preInit
    /**
     * Returns an initiliazed instance of itemCommand component.
     * @return the initialized component instance
     */
    public Command getItemCommand() {
        if (itemCommand == null) {//GEN-END:|40-getter|0|40-preInit
            // write pre-init user code here
            itemCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|40-getter|1|40-postInit
        // write post-init user code here
        }//GEN-BEGIN:|40-getter|2|
        return itemCommand;
    }
    //</editor-fold>//GEN-END:|40-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|43-getter|0|43-preInit
    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|43-getter|0|43-preInit
            // write pre-init user code here
            exitCommand = new Command("Sair", Command.EXIT, 0);//GEN-LINE:|43-getter|1|43-postInit
        // write post-init user code here
        }//GEN-BEGIN:|43-getter|2|
        return exitCommand;
    }
    //</editor-fold>//GEN-END:|43-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|45-getter|0|45-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|45-getter|0|45-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|45-getter|1|45-postInit
        // write post-init user code here
        }//GEN-BEGIN:|45-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|45-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|47-getter|0|47-preInit
    /**
     * Returns an initiliazed instance of exitCommand1 component.
     * @return the initialized component instance
     */
    public Command getExitCommand1() {
        if (exitCommand1 == null) {//GEN-END:|47-getter|0|47-preInit
            // write pre-init user code here
            exitCommand1 = new Command("Sair", Command.EXIT, 0);//GEN-LINE:|47-getter|1|47-postInit
        // write post-init user code here
        }//GEN-BEGIN:|47-getter|2|
        return exitCommand1;
    }
    //</editor-fold>//GEN-END:|47-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: list ">//GEN-BEGIN:|50-getter|0|50-preInit
    /**
     * Returns an initiliazed instance of list component.
     * @return the initialized component instance
     */
    public List getList() {
        if (list == null) {//GEN-END:|50-getter|0|50-preInit
            // write pre-init user code here
            list = new List("Menu", Choice.IMPLICIT);//GEN-BEGIN:|50-getter|1|50-postInit
            list.append("Play", null);
            list.append("Instructions", null);
            list.append("Quit", null);
            list.setCommandListener(this);
            list.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            list.setSelectedFlags(new boolean[] { false, false, false });//GEN-END:|50-getter|1|50-postInit
        // write post-init user code here
        }//GEN-BEGIN:|50-getter|2|
        return list;
    }
    //</editor-fold>//GEN-END:|50-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: listAction ">//GEN-BEGIN:|50-action|0|50-preAction
    /**
     * Performs an action assigned to the selected list element in the list component.
     */
    public void listAction() {//GEN-END:|50-action|0|50-preAction
        // enter pre-action user code here

        switch (getList().getSelectedIndex()) {//GEN-BEGIN:|50-action|1|53-preAction
            case 0://GEN-END:|50-action|1|53-preAction
                this.gameCanvas = new TetrisGameCanvas();
                final Display display = Display.getDisplay(this);
                display.setCurrent(gameCanvas);
                new Thread(gameCanvas).start();
            // write pre-action user code here
//GEN-LINE:|50-action|2|53-postAction
            // write post-action user code here
                break;//GEN-BEGIN:|50-action|3|54-preAction
            case 1://GEN-END:|50-action|3|54-preAction
                // write pre-action user code here
//GEN-LINE:|50-action|4|54-postAction
                // write post-action user code here
                break;//GEN-BEGIN:|50-action|5|55-preAction
            case 2://GEN-END:|50-action|5|55-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|50-action|6|55-postAction
            // write post-action user code here
                break;//GEN-BEGIN:|50-action|7|50-postAction
        }//GEN-END:|50-action|7|50-postAction
    // enter post-action user code here
    }//GEN-BEGIN:|50-action|8|
    //</editor-fold>//GEN-END:|50-action|8|



















    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
}
